<template>
  <el-upload
      v-model:file-list="props.fileList"
      :action="uploadUrl"
      :multiple="true"
      :show-file-list="false"
      :before-upload="beforeFileUpload"
      :on-success="handleUploadSuccess"
      style="display: none"
  >
    <template #trigger>
      <div ref="uploadTrigger"></div>
    </template>
  </el-upload>
</template>

<script setup>
import { ref, defineProps, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  fileList: {
    type: Array,
    required: true
  }
})

const emits = defineEmits(['success'])

const uploadUrl = import.meta.env.VITE_BASE_URL + "/files/upload"

const beforeFileUpload = (file) => {
  const maxSize = 1024 * 1024 * 200 // 200MB
  if (file.size > maxSize) {
    ElMessage.error('文件大小不能超过200MB')
    return false
  }
  return true
}

const handleUploadSuccess = (response) => {
  if (response.code === 200) {
    emits('success', response)
  }
}
</script>